﻿/// SetScreenSize nếu có thay đổi kích thước screen.
/// SetMapSize nếu có thay đổi kích thước map.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using CastleDefender.ConfigFolder;

namespace CastleDefender.Platform.ScreenBehavior
{
    public class Camera : DrawableGameComponent
    {
        //
        #region VARIABLE
        private TouchBehavior myTouch;
        private Vector2 vecScreenSize;
        private Vector2 vecMapSize;
        #endregion

        #region CONSTRUCTOR
        public Camera(Game myGame, TouchBehavior myTouch) :base(myGame)
        {
            Position = new Vector2(Config.CameraDefaultPosition.X, Config.CameraDefaultPosition.Y);
            vecScreenSize = new Vector2(Config.ScreenSize.X, Config.ScreenSize.Y);
            vecMapSize = new Vector2(Config.MapSize.X, Config.MapSize.Y);
            this.myTouch = myTouch;
        }
        #endregion 

        #region PROPERTIES
        public Vector2 Position { get; set; }
        #endregion

        #region PULIC METHOD
        public Matrix TransformMatrix
        {
            get
            {
                return Matrix.CreateTranslation(-Position.X, -Position.Y, 0)
                *Matrix.CreateTranslation(new Vector3(vecScreenSize.X, vecScreenSize.Y, 0));
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (myTouch.IsControlled) return;
            if (myTouch.IsMoved)
            {
                Position += myTouch.Delta;
                FixPosition();

                myTouch.SetConvertVector(Config.ScreenSize - Position);
            }
            base.Update(gameTime);
        }

        public void SetScreenSize(Vector2 screenSize)
        {
            this.vecScreenSize = screenSize;
        }

        public void SetMapSize(Vector2 mapSize)
        {
            this.vecMapSize = mapSize;
        }
        #endregion

        #region PRIVATE METHOD
        private bool FixPosition()
        {
            if (Position.X > vecMapSize.X) Position = new Vector2(vecMapSize.X, Position.Y);
            if (Position.Y > vecMapSize.Y) Position = new Vector2(Position.X, vecMapSize.Y);
            if (Position.X - vecScreenSize.X < 0) Position = new Vector2(vecScreenSize.X, Position.Y);
            if (Position.Y - vecScreenSize.Y < 0) Position = new Vector2(Position.X, vecScreenSize.Y);

            return true;
        }
        #endregion
    }
}
